package sufutian.com.frameworkdemo.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import sufutian.com.frameworkdemo.network.netwrok2.BaseDataSource;
import sufutian.com.frameworkdemo.network.netwrok2.DataSourceErrorType;
import sufutian.com.frameworkdemo.network.netwrok2.INetworkDataSource;

/**
 * Created by sufutian on 2017/6/6.
 */

public class OkHttpEngine extends BaseDataSource implements INetworkDataSource,Callback {

    private static OkHttpEngine insstance;

    private String url;
    private IJson json;
    private static OkHttpClient httpClient;
    private RequestBody body;
    private Request request;



    private Context context;

    private enum HTTP_TYPE {
        GET,
        POST
    }

    private OkHttpEngine() {
    }





    public static OkHttpEngine getOkHttp() {
        if (insstance == null) {
            synchronized (OkHttpEngine.class) {
                if (insstance == null) {
                    insstance = new OkHttpEngine();
                }
            }
        }
        return insstance;
    }


    public void initOk(Context context) {
        this.context = context;
        httpClient = new OkHttpClient.Builder()
                .readTimeout(1000, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(1000, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(3000, TimeUnit.SECONDS)//设置连接超时时间
                .build();

    }

    /**
     * 开始连接网络
     */
    private void connect() {
        /**
         * 读取缓存
         */
//        toReadCacheOrNot(json, server);

        if (!isNetworkValidate()) {
            if (netListener != null) {
                netListener.onDataFail(DataSourceErrorType.network_error, "当前网络不可用","");
            }
            return;
        }

        if (TextUtils.isEmpty(url)) {
            if (netListener != null) {
                netListener.onDataFail(DataSourceErrorType.parameter_error, "请设置url","");
            }
            return;
        }

        if (json == null) {
            if (netListener != null) {
                netListener.onDataFail(DataSourceErrorType.parameter_error, "请设置Json","");
            }
            return;
        }
        if (httpClient != null) {
//            httpClient.cancle(request);
        }

        if (netListener != null) {
            netListener.onLoading();
        }

//        request = request(json, url, "");
    }

//    private Request request(IJson json, String url, String s) {
//
//    }


    public void loadGet(String url, final NetCallBack netCallback) {
        request = new Request.Builder()
                .url(url)
                .build();

        check(netCallback);

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }


    public void check(NetCallBack callBack) {

    }


    private boolean isNetworkValidate() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo netWorkInfo = cm.getActiveNetworkInfo();
            if (netWorkInfo != null) {
                return netWorkInfo.isAvailable();
            }
        }
        return false;
    }


    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {

    }



    @Override/*BaseDataSource*/
    public void get() {

    }

    @Override/*BaseDataSource*/
    public void post() {

    }

    @Override/*BaseDataSource*/
    public void cancel() {

    }

    @Override/*INetworkDataSource*/
    public void seturl(String server) {
        this.url=server;
    }

    @Override/*INetworkDataSource*/
    public void setJson(IJson json) {
        this.json=json;
    }

    @Override/*INetworkDataSource*/
    public void setBody(RequestBody body) {
        this.body = body;
    }
}
